package com.inspector.app.ui.chat.holder;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.inspector.app.BaseApp;
import com.inspector.app.R;
import com.inspector.app.ui.chat.adapter.ChatAdapter;
import com.inspector.app.ui.chat.entity.ReceiveMessagesEntity;
import com.inspector.app.ui.widget.CircleImageView;
import com.inspector.app.ui.chat.utils.DateUtil;

import org.json.JSONObject;

public class ItemAudioReceiveHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private TextView itemTime;
    private TextView name;
    private CircleImageView itemHeader;
    private RelativeLayout rlAudio;
    private TextView tvDuration;
    private ImageView ivAudio;
    private String url;

    private AnimationDrawable animation;
    private ChatAdapter.OnItemListener mListener;
    private int position;
    private RelativeLayout itemLayout;
    private TextView messageRetraction;
    private ReceiveMessagesEntity mEntity;
//    private MediaPlayer mMediaPlayer;

    public ItemAudioReceiveHolder(@NonNull View itemView) {
        super(itemView);
//        if (mMediaPlayer == null) {
//            mMediaPlayer = new MediaPlayer();
//        }
        itemTime = itemView.findViewById(R.id.item_tv_time);
        name = itemView.findViewById(R.id.name);
        itemHeader = itemView.findViewById(R.id.chat_item_header);
        rlAudio = itemView.findViewById(R.id.rlAudio);
        tvDuration = itemView.findViewById(R.id.tvDuration);
        ivAudio = itemView.findViewById(R.id.ivAudio);
        itemLayout = itemView.findViewById(R.id.item_layout);
        messageRetraction = itemView.findViewById(R.id.message_retraction);
    }

    public void setOnItemListener(ChatAdapter.OnItemListener listener, int position) {
        this.mListener = listener;
        this.position = position;
    }

    @SuppressLint("SetTextI18n")
    public void bind(ReceiveMessagesEntity entity) {
        if (entity != null) {
            if (entity.gettShow() == 1) {
                itemLayout.setVisibility(GONE);
                itemTime.setVisibility(GONE);
                messageRetraction.setVisibility(VISIBLE);
            } else {
                mEntity = entity;
                itemLayout.setVisibility(VISIBLE);
                itemTime.setVisibility(VISIBLE);
                messageRetraction.setVisibility(GONE);
                itemTime.setVisibility(entity.getCreateTime() == 0 ? GONE : VISIBLE);

                String standardTimes = DateUtil.getStandardTimes(entity.getCreateTime());
                itemTime.setText(standardTimes);

                String position = "";
                int source = entity.getSource();
                switch (source) {
                    case 1:
                        position = "指挥员";
                        break;
                    case 2:
                        position = "飞行员";
                        break;
                    case 3:
                        position = "观察员";
                        break;
                }

                name.setText(entity.getSendUserName() + "(" + position + ")");

                Glide.with(BaseApp.getInstance())
                        .load(entity.getSendHeadPic())
                        .apply(new RequestOptions().circleCrop())
                        .into(itemHeader);
                animation = (AnimationDrawable) ivAudio.getBackground();
                rlAudio.setOnClickListener(this);


                String message = entity.getMessage();
                try {
                    JSONObject jsonObject = new JSONObject(message);
                    url = jsonObject.getString("content");
                    if (TextUtils.isEmpty(url)) return;
//                    mMediaPlayer.setDataSource(url);
//                    mMediaPlayer.prepare();
//                    int totalDuration = mMediaPlayer.getDuration();
                    tvDuration.setText((entity.getAudioTime() / 1000) % 60 + "''");
//                    mMediaPlayer.release();
                } catch (Exception e) {

                }

            }


        }

    }


    @Override
    public void onClick(View view) {
        if (mListener != null)
            mListener.startAudioListener(animation, url, position);
    }


}
